<!--
 (c) Copyright Ascensio System SIA 2020

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Hello world</title>
    <script type="text/javascript" src="./plugins.js"></script>
    <script type="text/javascript" src="./plugins-ui.js"></script>
    <link rel="stylesheet" href="./plugins.css" />
    <script type="text/javascript">

        // 避免绑定多次
               if(!window['boundInternalcommand']){

                // window.Asc.plugin.info.options // 获取 editorConfig.plugins.options 的数据

                window.parent.addEventListener('message', (e) => {
                    const _data = e.data;
            
                    if(typeof _data !== 'string') {
                        return;
                    }
                    if(!_data.startsWith('{')) {
                        return;
                    }
            
                    const dataObj = JSON.parse(_data);
            
                    if(dataObj.ctype !== 'hzero') {
                        return;
                    }

                    if(window.Asc.plugin.info && window.Asc.plugin.info.options) {
                        if(window.Asc.plugin.info.options.divId !== dataObj.divId) {
                            return;
                        }
                    }

                    let callBack = undefined;

                    if(dataObj.uuid) {
                        callBack = (...args) => {
                            const doObj = {
                                ctype: 'hzero-callback',
                                uuid: dataObj.uuid,
                                args,
                                divId: dataObj.divId
                            };
                            window.top.postMessage(JSON.stringify(doObj), '*');
                        };
                    }
            
                    // 如果需要自定义插件的功能, 只需要在这个方法里接受参数写逻辑就行
                    var command = dataObj.cmd;
                    if(command === 'executeDomApi') {
                        const res = eval(dataObj.args[0]);
                        if(callBack) {
                            callBack(res);
                        }
                    } else if(command === 'executeCommand') {
                        window.Asc.plugin.executeCommand(...(dataObj.args), callBack);
                    } else if (command === 'executeMethod') {
                        window.Asc.plugin.executeMethod(...(dataObj.args), callBack);
                    }
                });
                window['boundInternalcommand'] = true;
            }

    window.Asc.plugin.init = function()
    {
        const doObj = {
            ctype: 'hzero-callback-loaded',
            divId: window.Asc.plugin.info.options.divId
        };
        window.top.postMessage(JSON.stringify(doObj), '*');
    };

    window.Asc.plugin.button = function(id)
    {
        this.executeCommand("close", "");
    };
    // window.parent.document.querySelector('[data-layout-name=toolbar-plugins]').style.display = 'none';
    // window.parent.document.querySelector('[data-layout-name=toolbar-collaboration]').style.display = 'none';
    // window.parent.document.querySelector('#left-btn-comments')?.click();
    </script>
</head>
<body style="left:0px; top:0px; margin:0px; padding:0px;"></body>
</html>